package com.rchat.pocmini.activity.view.chat;

/**
 * Created by ljd on 2017/3/9.
 *
 *
 */


import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rchat.pocmini.R;

/**
 * 自定义标题栏
 *
 */
public class CustomTitleBar extends LinearLayout {

    private LayoutInflater mInflater;
    private View m_vTitleView;

    private LinearLayout m_layoutLeft,m_layoutRight;
    private TextView m_tvLeftTitle;
    private TextView m_tvCenterTitle;
    private TextView m_ivIconRight;
    private ImageView m_ivIconLeft,m_ivIconRightBtn;



    // 页面变量
    private Activity mActivity;
    private Resources mResources;
    private OnTitleBarBackListener mBackListener;

    public CustomTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomTitleBar(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        mResources = context.getResources();
        mInflater = LayoutInflater.from(context);
        m_vTitleView = mInflater.inflate(R.layout.widget_custom_titlebar, null);
        m_layoutLeft = (LinearLayout) m_vTitleView.findViewById(R.id.layout_widget_custom_titlebar_left);
        m_layoutRight = (LinearLayout) m_vTitleView.findViewById(R.id.layout_widget_custom_titlebar_right);

        m_tvLeftTitle = (TextView) m_vTitleView.findViewById(R.id.tv_widget_custom_titlebar_left_title);
        m_tvCenterTitle = (TextView) m_vTitleView.findViewById(R.id.tv_widget_custom_titlebar_title);

        m_ivIconRight = (TextView) m_vTitleView.findViewById(R.id.iv_widget_custom_titlebar_right);
        m_ivIconRightBtn = (ImageView) m_vTitleView.findViewById(R.id.iv_widget_custom_titlebar_right_btn);
        m_ivIconLeft = (ImageView) m_vTitleView.findViewById(R.id.iv_widget_custom_titlebar_left);

        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        addView(m_vTitleView, llp);
    }

    public void setActivity(Activity activity, OnTitleBarBackListener onTitleBarBackListener) {
        mActivity = activity;
        mBackListener = onTitleBarBackListener;
        if (mActivity != null) {
            m_layoutLeft.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mActivity.getCurrentFocus()!=null)
                    {
                        ((InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE))
                                .hideSoftInputFromWindow(mActivity.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                    }

                    if (mBackListener != null) {
                        mBackListener.onBtnBackPressed();
                    } else {
                        mActivity.finish();
                    }
                }
            });
        }
    }

    /**
     * 设置右边按钮点击事件
     */
    public void setRightBtnOnClickListener(OnClickListener listener) {
        if (listener == null) {
            return;
        }
        m_layoutRight.setOnClickListener(listener);
//		m_ivIconRight.setOnClickListener(listener);
//		m_ivIconRightBtn.setOnClickListener(listener);
    }

    public void setActivity(Activity activity) {
        setActivity(activity, null);
    }

    public TextView getCenterTitle() {
        return m_tvCenterTitle;
    }



    /**
     * 设置主标题
     */
    public void setCenterTitle(CharSequence text) {
        m_tvCenterTitle.setText(text);
        m_tvCenterTitle.setTextColor(Color.rgb(255, 255, 255));
        m_tvCenterTitle.setVisibility(View.VISIBLE);
    }

    /**
     * 设置主标题颜色
     */
    public void setCenterTitleColor(int res) {
        res = R.color.bg_white;
        m_tvCenterTitle.setTextColor(mResources.getColor(res));
        m_tvCenterTitle.setVisibility(View.VISIBLE);
    }

    /**
     * 设置主标题
     */
    public void setCenterTitle(int res) {
        m_tvCenterTitle.setText(mResources.getString(res));
        m_tvCenterTitle.setTextColor(Color.rgb(255, 255, 255));
        m_tvCenterTitle.setVisibility(View.VISIBLE);
    }

    public void hideLeftIcon() {
        m_ivIconLeft.setVisibility(View.GONE);
    }
    /**
     * 设置左边标题图标(没有设置为0)
     */
    public void setletticon(int res) {
        if(res==0){
            m_ivIconLeft.setImageResource(R.drawable.ic_blue_tittlebal_left);
        }else{
            m_ivIconLeft.setImageResource(res);
        }
    }

    public void setRightIcon(int res) {
        displayRightLayout(true);
//		m_ivIconRightb.setBackgroundResource(res);
//		m_ivIconRight.setVisibility(View.VISIBLE);
        m_ivIconRightBtn.setBackgroundResource(res);
        m_ivIconRightBtn.setVisibility(View.VISIBLE);
    }

    public void setRightText(String text,int color) {
        displayRightLayout(true);
        m_ivIconRight.setText(text);
        if (color!=0) {
            m_ivIconRight.setTextColor(color);
        }
        m_ivIconRight.setVisibility(View.VISIBLE);
    }

    public void setRightText(String text) {
        setRightText(text,0);
    }

    public void setLeftTitle(int res) {
        m_tvLeftTitle.setText(mResources.getString(res));
        m_tvLeftTitle.setTextColor(Color.rgb(255, 255, 255));
        displayLeftTitle(true);
    }
    public void setLeftTitle(String str) {
        m_tvLeftTitle.setText(str);
        m_tvLeftTitle.setTextColor(Color.rgb(255, 255, 255));
        displayLeftTitle(true);
    }

    /**
     * 显示左侧按钮
     */
    public void displayLeftTitle(boolean b) {
        if (b) {
            m_tvLeftTitle.setVisibility(View.VISIBLE);
        } else {
            m_tvLeftTitle.setVisibility(View.GONE);
        }
    }

    /**
     * 显示左侧按钮
     */
    public void displayRightLayout(boolean b) {
        if (b) {
            m_layoutRight.setVisibility(View.VISIBLE);
        } else {
            m_layoutRight.setVisibility(View.GONE);
        }
    }


    public interface OnTitleBarBackListener {
        public void onBtnBackPressed();
    }

}

